package com.course.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import com.course.bean.ExamTo;
import com.course.daoI.ExamDAOI;
import com.course.dbutil.DBConnectionFactory;
import com.course.dbutil.SqlConstants;
import com.course.util.DateWrapper;

public class ExamDaoImpl implements ExamDAOI{

	
	
	PreparedStatement pstmt,pstmt1;
	Statement stmt;
	ResultSet rs,rs1;
	Connection con;
	
	
	public ExamDaoImpl()
	{
		
	con=DBConnectionFactory.getConnection();
	
	}
	
	public boolean insertQuestion(ExamTo eb)
	{
		boolean flag=true;
		try{
			
			pstmt=con.prepareStatement(SqlConstants._INSERT_EXAM_QUESTION);
			
			
			String qtext=eb.getQtext();
			String ans1=eb.getAns1();
			String ans2=eb.getAns2();
			String ans3=eb.getAns3();
			String ans4=eb.getAns4();
			String cans=eb.getQans();
			
			pstmt.setString(1,qtext);
			pstmt.setString(2,ans1);
			pstmt.setString(3,ans2);
			pstmt.setString(4,ans3);
			pstmt.setString(5,ans4);
			pstmt.setString(6,cans);
			
			
			
			int insert=pstmt.executeUpdate();
			
			if(insert>0)
	        {
	        
	         con.commit();
	        }
	        else
	        {
	       	 flag=false;
	       	 con.rollback();
	        }

	   } 
	   
	   catch (SQLException e) 
	   {
	       e.printStackTrace();
	       flag=false;
	       try 
	       {
	           con.rollback();
	       } 
	       catch (SQLException se) 
	       {
	           se.printStackTrace();
	       }
	   }
	   
	   finally
		{
		 try{
			 if(con!=null)
				 con.close();				 
		 }
		 catch(SQLException e){}
		}
	return flag;
	}
	
	
	
	
	public Vector<ExamTo> viewQuestion()
	{
		
		
		Vector<ExamTo> veb=null;
		try{
			
			pstmt=con.prepareStatement(SqlConstants._SELECT_QUESTION );
			
			rs=pstmt.executeQuery();
			
			veb=new Vector<ExamTo>();
			
			while(rs.next())
			{
				
				int qno=rs.getInt(1);
				String qtext=rs.getString(2);
				String ans1=rs.getString(3);
				String ans2=rs.getString(4);
				String ans3=rs.getString(5);
				String ans4=rs.getString(6);
				String qans=rs.getString(7);
				
				
				ExamTo eb=new ExamTo();
				
				eb.setAns1(ans1);
				eb.setAns2(ans2);
				eb.setAns3(ans3);
				eb.setAns4(ans4);
				eb.setQans(qans);
				eb.setQtext(qtext);
				eb.setQno(qno);
				
				veb.add(eb);
				
				
			}
			
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		
		
		
		return veb;
	}

	
	public boolean scheduleExam(ExamTo eb)
	{
		boolean flag=true;
		try{
			
			pstmt=con.prepareStatement(SqlConstants._EXAM_SCHEDULE);
			
			
			String sub=eb.getSubj();
			String edate=eb.getExamdate();
			String day=eb.getDay();
			String time=eb.getTime();
			
			pstmt.setString(1,sub);
			pstmt.setString(2,DateWrapper.parseDate(edate));
			pstmt.setString(3,day);
			pstmt.setString(4,time);
			
			
			
         int insert=pstmt.executeUpdate();
			
			if(insert>0)
	        {
	        
	         con.commit();
	        }
	        else
	        {
	       	 flag=false;
	       	 con.rollback();
	        }

	   } 
	   
	   catch (Exception e) 
	   {
	       e.printStackTrace();
	       flag=false;
	       try 
	       {
	           con.rollback();
	       } 
	       catch (SQLException se) 
	       {
	           se.printStackTrace();
	       }
	   }
	   
	   finally
		{
		 try{
			 if(con!=null)
				 con.close();				 
		 }
		 catch(Exception e){}
		}
	return flag;
	}
	
	
	public Vector<ExamTo> viewSchedule()   
	{
		
		Vector<ExamTo> veb=null;
		
		try{
			
			pstmt=con.prepareStatement(SqlConstants._VIEW_SCHEDULE);
			
			
			rs=pstmt.executeQuery();
			veb=new Vector<ExamTo>();
			
			
			while(rs.next())
			{
				
				int eid=rs.getInt(1);
				String subj=rs.getString(2);
				String edate=rs.getString(3);
				String day=rs.getString(4);
				String time=rs.getString(5);
				
				
				ExamTo eb=new ExamTo();
				
				eb.setExamdate(edate);
				eb.setSubj(subj);
				eb.setDay(day);
				eb.setTime(time);
				eb.setEid(eid);
				
				veb.add(eb);
				
		}
			
	}
		catch (Exception e) {
		e.printStackTrace();
		}
		
		
		return veb;
		
}
	
	
	
	public boolean insertMarks(ExamTo eb)
	{
		boolean flag=true;
		
		int count=0;
		try{
			
			pstmt=con.prepareStatement(SqlConstants._INSERT_MARKS);
			
			
			
			String qno[]=eb.getQusetionno();
			
			String login=eb.getLogin();
			
			if(qno!=null)
			{
			for(int i=1;i<=qno.length;i++)
			{
			String ans=""+i;
			
			stmt=con.createStatement();
		
			ResultSet rs=stmt.executeQuery("select CORRECTANS from question where qno ="+qno[i-1]);
			if(rs.next())
			{
			String correct=rs.getString(1);
			if(ans!=null)
			{
			if(ans.equals(correct))
			{
			count++;
			
			}
			else
			{
			continue;
			 }
			   }
			  }
			}
			}
			
			pstmt.setInt(1,count);
			pstmt.setString(2,login);
			pstmt.executeUpdate();
			
			
              int insert=pstmt.executeUpdate();
			
			if(insert>0)
	        {
	        
	         con.commit();
	        }
	        else
	        {
	       	 flag=false;
	       	 con.rollback();
	        }

			}
	   
	   catch (Exception e) 
	   {
	       e.printStackTrace();
	       flag=false;
	       try 
	       {
	           con.rollback();
	       } 
	       catch (SQLException se) 
	       {
	           se.printStackTrace();
	       }
	   }
	   
	   finally
		{
		 try{
			 if(con!=null)
				 con.close();				 
		 }
		 catch(Exception e){}
		}
	return flag;
	}
	
	
	
	
	public Vector<ExamTo > viewMarks()
	{
		
		Vector<ExamTo > veb=null;
		try{
			
			
			
			pstmt=con.prepareStatement(SqlConstants._VIEW_MARKS);
			
			rs=pstmt.executeQuery();
			
			veb=new Vector<ExamTo>();
			while(rs.next())
			{
				
				
				int marks=rs.getInt(1);
				String sname=rs.getString(2);
				String edate=rs.getString(3);
				
				
				ExamTo eb=new ExamTo();
				
				eb.setMarks(marks);
				eb.setSname(sname);
				eb.setExamdate(edate);
				
				veb.add(eb);
				
				}
			
			
	}
		catch (Exception e) {
			e.printStackTrace();
		}
		
		
		
		
		return veb;
		
		
		
	}
	
	
	
	public Vector<ExamTo > viewMarksAt(String login)
	{
		
		Vector<ExamTo > veb=null;
		try{
			
			
			
			pstmt=con.prepareStatement(SqlConstants._VIEW_MARKS_AT);
			
			pstmt.setString(1, login);
			rs=pstmt.executeQuery();
			
			veb=new Vector<ExamTo>();
			while(rs.next())
			{
				
				
				int marks=rs.getInt(1);
				String sname=rs.getString(2);
				String edate=rs.getString(3);
				
				
				ExamTo eb=new ExamTo();
				
				eb.setMarks(marks);
				eb.setSname(sname);
				eb.setExamdate(edate);
				
				veb.add(eb);
				
				}
			
			
	}
		catch (Exception e) {
			e.printStackTrace();
		}
		
		
		
		
		return veb;
		
		
		
	}
	
}
